package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Apply;
import com.qy.model.Evaluation;
import com.qy.model.Member;
import com.qy.model.Policy;
import com.qy.service.ApplyService;
import com.qy.service.EvaluationService;
import com.qy.service.MemberService;
import com.qy.service.PolicyService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-ApplyController")
@RequestMapping("/manage/apply")
public class ApplyController {
    @Resource
    private ApplyService applyService;

    @Resource
    private MemberService memberService;

    @Resource
    private PolicyService policyService;

    @Resource
    private EvaluationService evaluationService;

    @PostMapping("/add")
    public Result add(@RequestBody Apply apply) {
        applyService.save(apply);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        applyService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Apply apply) {
        applyService.update(apply);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Apply apply = applyService.findById(id);
        return ResultGenerator.successResult(apply);
    }

    @GetMapping("/list")
    public Result list(PageBean<Apply> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Apply> list = applyService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //申请管理-申请列表查询
    @GetMapping("applyList")
    public Result applyList(PageBean<Apply> page,Integer type , String content){
        Apply apply = new Apply();
        if (type == 1){
            apply.setApplicant(content);
        }
        if (type == 2){
            apply.setTelphone(content);
        }
        if (type == 3){
            apply.setPolicy_name(content);
        }
        PageHelper.startPage(page.getPageNum(), page.getSize() , "add_time desc");
        List<Apply> list = applyService.applyList(apply);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //申请管理-查看
    @GetMapping("seeApply")
    public Result seeApply(Integer id){
        if (id ==null || id == 0){
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Map map = applyService.seeApply(id);
        return ResultGenerator.successResult(map);
    }
}
